Ext.namespace('Ext.publics'); //包名；命名空间（防止重名）
dataType = [];
Ext.publics.CardFormPanel = function(viewer , config) {
	this.viewer = viewer;
    Ext.apply(this, config);
    this.items = this.loadFormItem();
    this.url = "business!save.action";
    Ext.publics.CardFormPanel.superclass.constructor.call(this, {
    	id:'iformpanel',
    	region:'center',
		monitorValid:true,
		//labelWidth:(pageparam.labelWidth ? pageparam.labelWidth : 85),
		baseCls:"x-plain",
		autoScroll:true,
		items:[this.items],
		buttonAlign: 'left', 
		buttons :[{
            text: '保&nbsp;&nbsp;&nbsp;存',
            iconCls: 'save32',
            iconAlign: 'top',
            scale: 'medium',
            scope:this,
            handler:this.submit
        },'-',{
            text: '重&nbsp;&nbsp;&nbsp;置',
            iconCls: 'reset32',
            scale: 'medium',
            iconAlign: 'top',
            scope:this,
            handler:this.reset
        },'->',{
            text: '关&nbsp;&nbsp;&nbsp;闭',
            iconCls: 'close32',
            scale: 'medium',
            iconAlign: 'top',
            handler:this.close
        }]
    })
};
	
Ext.extend(Ext.publics.CardFormPanel, Ext.form.FormPanel, {
	submit:function(_params){
		var waitMsg = "正在保存数据，稍后...";
		var waitTitle = "数据传送";
		var tableName = ("undefined" == typeof cardparam.businame) ? pageparam.businame : cardparam.businame;
		var data = this.getValues();
		var _params = this.handlerParams(tableName,data,cardparam.savetype);
		var cjson = Ext.util.JSON.encode(_params);
		if (this.checkValid()){
            this.getForm().submit({url:this.url , 
            					   method:'POST',
								   params:{saveConfig:cjson}, 
								   success:this.success, 
								   waitTitle:waitTitle, 
								   waitMsg:waitMsg,	
								   failure:this.failure,
								   scope:this}) ;
		}else{
		    Ext.Msg.alert('提示信息', '请填写完成后再提交!');
		}
	},
	//操作成功
	success:function(){
		Ext.MessageBox.alert('提示信息', '操作成功！');
		this.close();
	},
	//操作失败
	failure:function(){
		Ext.MessageBox.alert('提示信息', '操作失败！');
	},
	//数据保存前检查数据有效性接口
	checkValid:function(){
		return this.getForm().isValid();
	},
	//数据保存前对数据进行处理接口
	handlerData:function(collectionData){
		if(collectionData){
			var index = 0;
			for( var i in collectionData){
				if(dataType[index] == "date"){
					var value = new Date(collectionData[i]);
					collectionData[i] = value ? value.dateFormat(dateFormat) : ''
				}
				index++;
			}
			return collectionData;
		}else{
			return {data:""};
		}
	},
	//设置传递参数
	handlerParams:function(tableName,data,option,cond){
		var params = {};
		var flds = [];
		var values = [];
		if(tableName && tableName.length > 0 && option && option.length > 0){
			params.tableName = tableName;
			params.option = option;
			if(option != CardOption["add"]){
				params.cond = cond;
			}
			for (var f in data){
				if (!f) {continue;}
				flds.push(f);
				var val = data[f];
				if (!val) val = "";
				values.push(val);
			}
		}
		params.flds = flds;
		params.values = values;
		return params;
		
	},
	//获取form数据
	getValues:function(){
		var collectionData = this.getForm().getFieldValues();
		var data = this.handlerData(collectionData);
		return data;
	},
	//重置
	reset:function(){
		this.getForm().reset() ;
	},
	//关闭
	close:function(){
		cardWindow.hide();
	},
	loadFormItem:function() {
		this.otherdatashowobj = {};
		var retval = [];
        for(var i = 0, forms; forms = this.FORMS[i]; i++){
			forms.id = 'form-fielsset-' + i;
			var dbname = forms.dbname;
			var fields = forms.fields;
			var defColWidth = forms.col ? forms.col : 2;
			this.otherdatashowobj[dbname] = {};
			var fieldset = {
				xtype:'fieldset',
		        layout:'column',
		        title:forms.title,
		        overCls :'form-field-over',
		        iconCls: 'form-field-flag',
		        style:"margin:5px;",
		        collapsible: true,
		       // width: document.body.clientWidth - 40,
		        defaults: { 
		        	baseCls:"x-plain",
		        	layout: 'form',
		        	style:"padding:3px 0px 3px 25px;",
		        	labelSeparator :':'
		        },
		        items:[]
			};
			if(fields){
				for(var j=0,_field;_field = fields[j];j++){
					var name = _field.name;
					var key = _field.key;
					if(key) this.key[name] = key;
					var itemDefColWidth = _field.col;
					itemDefColWidth = itemDefColWidth + ((itemDefColWidth -1) * 0.064);
					var colWidth = 1 / defColWidth;
					if(itemDefColWidth && itemDefColWidth!="1") colWidth = itemDefColWidth * colWidth;
					if(_field.stype != "photoView"  && _field.stype != "iframeHTML") this.otherdatashowobj[dbname][name] = name;
					var item = this.createItem(_field,forms);
					var overcls = (_field.stype != "photoView" && _field.stype != "iframeHTML") ? "form-field-item-over" : "";
					var field = {
						columnWidth:colWidth, // 并且必须是一个大于0小于1的十进制值(例如.25)。 
						defaults: { overCls :overcls,anchor:"94%"},
						items:[item]
					};
            		fieldset.items.push(field);
				}
			}
			retval.push(fieldset);
		}			
    	return retval;
	},
	createItem : function (fobj,pfobj) {
		var retObj = {};
		var stype = fobj.stype;
		dataType[dataType.length] = stype;
		switch (stype){
			case "text":
				retObj = this.createText(fobj, pfobj);
				break;
		}
		
    	return retObj;
	},
	createText : function( aobj , pfobj ){
		var alabel = aobj.label;
		var aname = aobj.name;
		var atype = aobj.vtype;
		var ablank = aobj.blank;
		var abText = aobj.blanktext;
		var adeftxt = aobj.def;
		var areadonly = aobj.readonly;
		var disabled = aobj.disabled;
		var hidden = aobj.hidden;
		var unique = aobj.unique;
		if (!alabel) {return {};}
		if (!aname) {return {};}
		
		if (aobj.isblank) {
			alabel = alabel + "...";
			alabel = Ext.util.Format.ellipsis(alabel,alabel.length - 2);
		};
		
		if (ablank==false) {
			alabel = alabel + "<font color=red>&nbsp;*</font>";
			if(!abText) abText = aname + "不能为空";
		}
		var retobj = {
			xtype:hidden?"hidden":"textfield",
			readOnly:areadonly,
			fieldLabel:alabel,
			labelWidth: 201,
			disabled:disabled,
			id: aname,
			name:aname,
			allowBlank : ablank,
			value:adeftxt,
			blankText:abText,
			labelStyle: (pageparam.labelAlign ? ('text-align:' + pageparam.labelAlign) : 'text-align:right'),
			listeners:{
				blur: function(tf){
					if(unique){
						var value = tf.getValue()+"";
						if(value.length>0){
							Ext.Ajax.request({
								url: "/xbsis/server/public/busicard_Array_Ext.jsp",
								method: 'POST',
								params: {jsons:Ext.encode([{option:'checkUnique', bname:pfobj.dbname, cond:aname+"='"+value+"'"}])},
								success: function(response, result){
									var result = eval("("+response.responseText+")");
									var count = Number(result.success.replace(/\[|\]|\"/g,""));
									if(count>0){
										Ext.Msg.alert("系统提示", aname+"已存在,请重新填写");
										tf.setValue("");
									}
								},
								failure: function(){
									Ext.Msg.alert("系统提示", "验证唯一性请求失败，请稍后重试");
								}
							});
						}
					}
				}
			}
		};
		if (atype) if (atype.length>0) retobj["vtype"] = atype;
		return retobj;
	}
});